package com.qlogic.commons.utils.reflect;

import java.io.File;

import com.qlogic.commons.exceptions.BaseException;

public class AdequateCompiler {
	
	public static final String COMPILER = "ql.compiler";
	
	public static AbstractJavaCompiler compiler;
	
	public static synchronized String compileClass(File sourceFile, String classpath)
	throws BaseException {
		if (compiler == null) {
			String compilerClass = System.getProperty (COMPILER);
			if (compilerClass != null) {
				try {
					compiler = (AbstractJavaCompiler)
						AdequateCompiler.class.getClassLoader().loadClass (compilerClass).newInstance();
				} catch (Exception e) {
					throw new BaseException(e);
				} 
			} else {
				compiler = new Jdk13Compiler ();
			}
		}
		
		return compiler.compileClass (sourceFile, classpath);
		
	}

}
